package com.example.cameralib


import android.os.Bundle
import android.view.TextureView
import android.view.View
import android.widget.ImageView
import com.camera.JYCamera
import com.example.baseutils.activity.BaseActivity

class CameraActivity : BaseActivity() {
    private lateinit var jyCamera: JYCamera
    private val cameraId = 0
    private var rotate = 0f
    private var  iv_bitmap :ImageView?=null
    private var ttv_camera:TextureView?=null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_camera)
        initData()
    }

    private fun initData() {
        iv_bitmap=findViewById(R.id.iv_bitmap)
        ttv_camera=findViewById(R.id.ttv_camera)
        jyCamera = JYCamera.Builder(this).setCameraType(cameraId).setCameraPictureSize(1920, 1080)
            .setCameraPictureSize(1920, 1080)
            .setTakeCallback { bytes, bitmap, i ->
                runOnUiThread {
                    iv_bitmap?.setImageBitmap(bitmap)
                }
            }.build()
    }

    override fun onResume() {
        super.onResume()
        jyCamera.doStartPreview(cameraId, ttv_camera)
    }

    override fun onPause() {
        super.onPause()
        jyCamera.doStopCamera()
    }

    override fun onDestroy() {
        jyCamera.doStopCamera()
        jyCamera.releaseAll()
        super.onDestroy()
    }

    fun takePhoto(view: View) {
        jyCamera.takePhoto()
    }

    fun mirror(view: View) {
        jyCamera.mirror()
    }

    fun rotate(view: View) {
        rotate += 90f
        if (rotate >= 360)
            rotate = 0f
        jyCamera.rotateDegree(rotate)

    }

    fun stopPreview(view: View) {
        jyCamera.doStopCamera()
    }

    fun startPreview(view: View) {
        jyCamera.doStartPreview(cameraId, ttv_camera)
    }
}